#include "EventLoop.h"
#include <iostream>
#include <memory>

using std::cout;
using std::endl;
using std::shared_ptr;

void onConnect(shared_ptr<TcpConnect> ptr)
{
    cout << ptr->toString() << " handle connect!" << endl;
}

void onMessage(shared_ptr<TcpConnect> ptr)
{
    cout << ">> msg from " << ptr->peerPort() << ": " << ptr->read();
    ptr->write("hello cpp\n");
}

void onClose(shared_ptr<TcpConnect> ptr)
{
    cout << ptr->toString() << " disconnect!" << endl;
}

void test()
{
    Acceptor acc{"127.0.0.1", 12345};
    acc.ready();
    EventLoop loop{acc};

    loop.setConnectCallback(std::move(onConnect));
    loop.setHandleMessage(std::move(onMessage));
    loop.setCloseCallback(std::move(onClose));

    loop.loop();
}

int main(int args, char *argv[])
{
    test();
    return 0;
}